﻿//using CSharp_OPTControllerAPI;
using NDK.AcqDevice;
using NDK.Module.Model;
using NDK.Module.Model.Quebec;
using NDK.Motion.Card;
using NDK.Motion.MachineManager;
using NDK.Motion.MachineResources;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
//using motormaster;

namespace NDK.Motion.BLL
{
    public static class Machine
    {
        #region 硬件实例
        public static KeyencePPG PPG = new KeyencePPG();

        public static OmronPLC OmronPLC_Up = new OmronPLC();

        public static OmronPLC OmronPLC_Down = new OmronPLC();

        public static OmronPLC OmronPLC_Hearb = new OmronPLC();

        public static OmronPLCFins omronPLCFins = new OmronPLCFins();

        public static KeyenceScanReader ScanReader = new KeyenceScanReader();

        public static Dictionary<string, ICarmera> DicCamera = new Dictionary<string, ICarmera>();
        public static Dictionary<string, double> DirExp = new Dictionary<string, double>();

        public static Dictionary<string, ILineLaser> DicLaser = new Dictionary<string, ILineLaser>();
        public static Dictionary<string, List<string>> DicLaserIp = new Dictionary<string, List<string>>();


        #endregion

        //Card
        public static Adlink AdlinkCard = new Adlink();

        public static ConcurrentDictionary<string, CheckVacuumClass> DicCheckVacuum { get; set; }
        static void AddDicCheckVacuum(string key, CheckVacuumClass value)
        {
            DicCheckVacuum.AddOrUpdate(key, value, (k, oldValue) => value);
        }
        public static ConcurrentDictionary<string, ImageLights> DicLight { get; set; }
        static void AddDicLight(string key, ImageLights value)
        {
            DicLight.AddOrUpdate(key, value, (k, oldValue) => value);
        }
        public static ConcurrentDictionary<string, InputSetting> DicInputIO { get; set; }
        static void AddDicInputIO(string key, InputSetting value)
        {
            DicInputIO.AddOrUpdate(key, value, (k, oldValue) => value);
        }
        public static ConcurrentDictionary<string, InputSetting> DicInputIODoor { get; set; }
        static void AddDicInputIODoor(string key, InputSetting value)
        {
            DicInputIODoor.AddOrUpdate(key, value, (k, oldValue) => value);
        }
        public static ConcurrentDictionary<string, InputSetting> DicInputIOPause { get; set; }
        static void AddDicInputIOPause(string key, InputSetting value)
        {
            DicInputIOPause.AddOrUpdate(key, value, (k, oldValue) => value);
        }
        public static ConcurrentDictionary<string, InputSetting> DicInputIODoorButton { get; set; }
        static void AddDicInputIODoorButton(string key, InputSetting value)
        {
            DicInputIODoorButton.AddOrUpdate(key, value, (k, oldValue) => value);
        }

        public static ConcurrentDictionary<string, Stopwatch> DicswIODoorButton { get; set; }
        static void AddDicswIODoorButton(string key, Stopwatch value)
        {
            DicswIODoorButton.AddOrUpdate(key, value, (k, oldValue) => value);
        }

        public static ConcurrentDictionary<string, InputSetting> DicInputIOStart { get; set; }
        static void AddDicInputIOStart(string key, InputSetting value)
        {
            DicInputIOStart.AddOrUpdate(key, value, (k, oldValue) => value);
        }

        public static ConcurrentDictionary<string, InputSetting> DicInputIOReset { get; set; }
        static void AddDicInputIOReset(string key, InputSetting value)
        {
            DicInputIOReset.AddOrUpdate(key, value, (k, oldValue) => value);
        }

        public static ConcurrentDictionary<string, InputSetting> DicInputIOScan { get; set; }
        static void AddDicInputIOScan(string key, InputSetting value)
        {
            DicInputIOScan.AddOrUpdate(key, value, (k, oldValue) => value);
        }
        public static ConcurrentDictionary<string, OutputSetting> DicOutputIO { get; set; }
        static void AddDicOutputIO(string key, OutputSetting value)
        {
            DicOutputIO.AddOrUpdate(key, value, (k, oldValue) => value);
        }

        public static ConcurrentDictionary<string, MeasureProduct> DicLocalData { get; set; }
        public static void AddDicLocalData(string key, MeasureProduct value)
        {
            DicLocalData.AddOrUpdate(key, value, (k, oldValue) => value);
        }

        public static ConcurrentDictionary<string, ICylinder> DicDriveCylinder { get; set; }
        static void AddDicDriveCylinder(string key, ICylinder value)
        {
            DicDriveCylinder.AddOrUpdate(key, value, (k, oldValue) => value);
        }

        public static ConcurrentDictionary<string, Dictionary<string, Position>> DicPos { get; set; }
        static void AddDicPos(string key, Dictionary<string, Position> value)
        {
            DicPos.AddOrUpdate(key, value, (k, oldValue) => value);
        }

        public static bool ImageLightInit()
        {
            if (MachineDataManager.Instance.ProjectImageLights.Count == 0)
                return false;
            string productType = Common.CurProType;
            if (productType.Contains("Block"))
            {
                productType = "Block";
            }
            var findItem = MachineDataManager.Instance.ProjectImageLights.Find(x => x.Name.Equals(productType));

            List<ImageLights> CurProjectImageLightslist;
            if (findItem != null)
                CurProjectImageLightslist = findItem.ImageLightsList;
            else
                return false;
            DicLight = new ConcurrentDictionary<string, ImageLights>();
            foreach (var light in CurProjectImageLightslist)
            {
                AddDicLight(light.ImageName, light);
            }
            Common.AppendUiLog.Log($"当前光源Source为{productType}", BZ.Logger.LogType.Alert);
            return true;
        }


        /// <summary>
        /// 资源初始化
        /// </summary>
        /// <returns></returns>

        public static void MachineResourceInit()
        {
            try
            {
                try
                {
                    DicCheckVacuum = new ConcurrentDictionary<string, CheckVacuumClass>();

                    DicInputIO = new ConcurrentDictionary<string, InputSetting>();
                    foreach (var item in MachineDataManager.Instance.DIList)
                    {
                        if (item.Name.Contains("真空反馈"))
                            AddDicCheckVacuum(item.Name, new CheckVacuumClass());
                        try
                        {
                            AddDicInputIO(item.Name, MachineDataManager.Instance.DIList.SingleOrDefault(d => d.Name == item.Name));
                        }
                        catch (Exception)
                        {

                            throw;
                        }
                       
                    }

                    DicInputIODoor = new ConcurrentDictionary<string, InputSetting>();
                    foreach (var item in DicInputIO.Keys)
                    {
                        if (item.Contains("门磁"))
                        {
                            AddDicInputIODoor(item, DicInputIO[item]);
                        }
                    }

                    DicInputIOPause = new ConcurrentDictionary<string, InputSetting>();
                    foreach (var item in DicInputIO.Keys)
                    {
                        if (item.Contains("停止按钮"))
                        {
                            AddDicInputIOPause(item, DicInputIO[item]);
                        }
                    }

                    DicInputIODoorButton = new ConcurrentDictionary<string, InputSetting>();
                    DicswIODoorButton = new ConcurrentDictionary<string, Stopwatch>();
                    foreach (var item in DicInputIO.Keys)
                    {
                        if (item.Contains("门请求") || item.Contains("门确认"))
                        {
                            AddDicInputIODoorButton(item, DicInputIO[item]);
                            AddDicswIODoorButton(item, new Stopwatch());
                        }
                    }

                    DicInputIOStart = new ConcurrentDictionary<string, InputSetting>();
                    foreach (var item in DicInputIO.Keys)
                    {
                        if (item.Contains("启动按钮"))
                        {
                            AddDicInputIOStart(item, DicInputIO[item]);
                        }
                    }

                    DicInputIOReset = new ConcurrentDictionary<string, InputSetting>();
                    foreach (var item in DicInputIO.Keys)
                    {
                        if (item.Contains("复位按钮"))
                        {
                            AddDicInputIOReset(item, DicInputIO[item]);
                        }
                    }

                    DicInputIOScan = new ConcurrentDictionary<string, InputSetting>();
                    foreach (var item in DicInputIO.Keys)
                    {
                        if (item.Contains("光栅"))
                        {
                            AddDicInputIOScan(item, DicInputIO[item]);
                        }
                    }

                    DicOutputIO = new ConcurrentDictionary<string, OutputSetting>();
                    foreach (var item in MachineDataManager.Instance.DOList)
                    {
                        try
                        {
                            AddDicOutputIO(item.Name, MachineDataManager.Instance.DOList.SingleOrDefault(d => d.Name == item.Name));
                        }
                        catch (Exception)
                        {

                            throw;
                        }
                       
                    }

                    DicPos = new ConcurrentDictionary<string, Dictionary<string, Position>>();
                    foreach (var item in MachineDataManager.Instance.PositionList)
                    {
                        if (!DicPos.ContainsKey(item.Station))
                            AddDicPos(item.Station, new Dictionary<string, Position>());
                        DicPos[item.Station].Add(item.PositionName, item);
                    }

                    DicDriveCylinder = new ConcurrentDictionary<string, ICylinder>();
                    foreach (var item in MachineDataManager.Instance.CylinderList)
                    {
                        AddDicDriveCylinder(item.Name, MachineDataManager.Instance.CylinderList.SingleOrDefault(d => d.Name == item.Name));
                    }

                    ImageLightInit();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                #region 未赋值对象判断
                List<string> 未成功赋值对象 = new List<string>();
                Type type = typeof(Machine);
                var Fields = type.GetFields();
                foreach (var item in Fields)
                {
                    var attr = item.GetCustomAttribute<MachinePropAttr>();
                    if (attr != null)
                    {
                        continue;
                    }
                    if (item.FieldType == typeof(InputSetting))
                    {
                        InputSetting input = (InputSetting)item.GetValue(null);
                        if (input == null)
                        {
                            未成功赋值对象.Add(item.Name);
                        }
                    }
                    else if (item.FieldType == typeof(OutputSetting))
                    {
                        OutputSetting ouput = (OutputSetting)item.GetValue(null);
                        if (ouput == null)
                        {
                            未成功赋值对象.Add(item.Name);
                        }
                    }
                    else if (item.FieldType == typeof(Position))
                    {
                        Position position = (Position)item.GetValue(null);
                        if (position == null)
                        {
                            未成功赋值对象.Add(item.Name);
                        }
                    }
                    else if (item.FieldType == typeof(ImageLights))
                    {
                        ImageLights imgLights = (ImageLights)item.GetValue(null);
                        if (imgLights == null)
                        {
                            未成功赋值对象.Add(item.Name);
                        }
                    }
                }
                StringBuilder sb = new StringBuilder();
                foreach (var item in 未成功赋值对象)
                {
                    sb.Append(item);
                    sb.Append(",");
                }
                if (sb.Length > 0)
                    sb.Remove(sb.Length - 1, 1);
                if (sb.Length > 0)
                    Common.AppendUiLog.Log($"未成功赋值：{sb}");
                #endregion
            }
            catch (Exception ex)
            {

            }
        }
    }
    public class CheckVacuumClass
    {
        public bool CheckStationVacuumbool = false;

        public DateTime CheckVacuumTime = DateTime.Now;
    }
}